/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.cd.cms.modules.mid.web;

import com.cd.cms.common.config.Global;
import com.cd.cms.common.persistence.Page;
import com.cd.cms.common.utils.MultipartFileToFileUtils;
import com.cd.cms.common.utils.StringUtils;
import com.cd.cms.common.utils.excel.ExportExcel;
import com.cd.cms.common.utils.excel.ImportExcel;
import com.cd.cms.common.web.BaseController;
import com.cd.cms.modules.mid.entity.GoodInfo;
import com.cd.cms.modules.mid.entity.TorderProduct;
import com.cd.cms.modules.mid.entity.TorderRquest;
import com.cd.cms.modules.mid.service.TorderProductService;
import com.cd.cms.modules.mid.service.TorderRquestService;
import com.google.common.collect.Lists;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单Controller
 *
 * @author XueD
 * @version 2016-05-10
 */
@Controller
@RequestMapping(value = "${adminPath}/mid/torderRquest")
public class TorderRquestController extends BaseController {

    @Autowired
    private TorderRquestService torderRquestService;


	@ModelAttribute
	public List<TorderRquest> getList(TorderRquest torderRquest) {
		return torderRquestService.getList(torderRquest);
	}
	
	@RequiresPermissions("mid:torderRquest:view")
	@RequestMapping(value = {"list", ""})
	public String list(TorderRquest torderRquest, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TorderRquest> page = torderRquestService.findPage(new Page<TorderRquest>(request, response), torderRquest); 
		model.addAttribute("page", page);
		return "modules/mid/torderRquestList";
	}
	//查看明细
	@RequiresPermissions("mid:torderRquest:view")
	@RequestMapping(value = "formDetail")
	public String formDetail(TorderRquest torderRquest, Model model) {
		model.addAttribute("torderRquest", torderRquest);
		TorderProduct torderProduct = new TorderProduct();
		torderProduct.settId(torderRquest.gettId());
		List<TorderProduct> list =  torderProductService.findList(torderProduct);
		model.addAttribute("page", list);
		return "modules/mid/torderRquestFormDetail";
	}
	//管理员修改订单
	@RequiresPermissions("mid:torderRquest:view")
	@RequestMapping(value = "form")
	public String form(TorderRquest torderRquest, Model model) {
		model.addAttribute("torderRquest", torderRquest);
		TorderProduct torderProduct = new TorderProduct();
		torderProduct.settId(torderRquest.gettId());
		List<TorderProduct> list =  torderProductService.findList(torderProduct);
		model.addAttribute("page", list);
		return "modules/mid/torderRquestForm";
	}
	
	@RequiresPermissions("mid:torderRquest:edit")
	@RequestMapping(value = "save")
	public String save(TorderRquest torderRquest, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, torderRquest)){
			return form(torderRquest, model);
		}
		torderRquestService.save(torderRquest);
		addMessage(redirectAttributes, "保存运单号成功");
		return "redirect:"+Global.getAdminPath()+"/mid/torderRquest/?repage";
	}
	
	@RequiresPermissions("mid:torderRquest:edit")
	@RequestMapping(value = "delete")
	public String delete(TorderRquest torderRquest, RedirectAttributes redirectAttributes) {
		torderRquestService.delete(torderRquest);
		addMessage(redirectAttributes, "删除订单成功");
		return "redirect:"+Global.getAdminPath()+"/mid/torderRquest/?repage";
	}


	@ModelAttribute
	public List<TorderRquest> getList(TorderRquest torderRquest) {
		return torderRquestService.getList(torderRquest);
	}
	
	@RequiresPermissions("mid:torderRquest:view")
	@RequestMapping(value = {"list", ""})
	public String list(TorderRquest torderRquest, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TorderRquest> page = torderRquestService.findPage(new Page<TorderRquest>(request, response), torderRquest); 
		model.addAttribute("page", page);
		return "modules/mid/torderRquestList";
	}
	//查看明细
	@RequiresPermissions("mid:torderRquest:view")
	@RequestMapping(value = "formDetail")
	public String formDetail(TorderRquest torderRquest, Model model) {
		model.addAttribute("torderRquest", torderRquest);
		TorderProduct torderProduct = new TorderProduct();
		torderProduct.settId(torderRquest.gettId());
		List<TorderProduct> list =  torderProductService.findList(torderProduct);
		model.addAttribute("page", list);
		return "modules/mid/torderRquestFormDetail";
	}
	//管理员修改订单
	@RequiresPermissions("mid:torderRquest:view")
	@RequestMapping(value = "form")
	public String form(TorderRquest torderRquest, Model model) {
		model.addAttribute("torderRquest", torderRquest);
		TorderProduct torderProduct = new TorderProduct();
		torderProduct.settId(torderRquest.gettId());
		List<TorderProduct> list =  torderProductService.findList(torderProduct);
		model.addAttribute("page", list);
		return "modules/mid/torderRquestForm";
	}
	
	@RequiresPermissions("mid:torderRquest:edit")
	@RequestMapping(value = "save")
	public String save(TorderRquest torderRquest, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, torderRquest)){
			return form(torderRquest, model);
		}
		torderRquestService.save(torderRquest);
		addMessage(redirectAttributes, "保存运单号成功");
		return "redirect:"+Global.getAdminPath()+"/mid/torderRquest/?repage";
	}

	  @RequiresPermissions("mid:torderRquest:edit")
	    @RequestMapping(value = "import", method = RequestMethod.POST)
	    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes, HttpServletRequest request) {
	        try {
	            String path = MultipartFileToFileUtils.multipartFileToFile(file, request);
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	        return "redirect:" + adminPath + "/mid/goodInfo/list?repage";
	    }

    @Autowired
    private TorderProductService torderProductService;

	private List<TorderRquest> readExcelToList(String filePath, String platformType){
		List<TorderRquest> list = new ArrayList<>();
		TorderRquest torderRquest ;
		try {
			ImportExcel ei = new ImportExcel(filePath, 0);
			for (int i = ei.getDataRowNum(); i <=ei.getLastDataRowNum(); i++) {
				Row row = ei.getRow(i);
				torderRquest = new TorderRquest();
				if(row.getCell(1)!=null){
					row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
					torderRquest.settId(row.getCell(1).getStringCellValue());
				}
				if(row.getCell(2)!=null){
					row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
					torderRquest.settClentName(row.getCell(2).getStringCellValue());
				}
				if(row.getCell(3)!=null){
					row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
					torderRquest.settPhoneNo(row.getCell(3).getStringCellValue());
				}
				if(row.getCell(4)!=null){
					row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
					torderRquest.settAddressProvince(row.getCell(4).getStringCellValue());
				}
				if(row.getCell(5)!=null){
					row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
					torderRquest.settAddressCity(row.getCell(5).getStringCellValue());
				}
				if(row.getCell(6)!=null){
					row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
					torderRquest.settAddressArea(row.getCell(6).getStringCellValue());
				}
				if(row.getCell(7)!=null){
					row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
					torderRquest.settAddressDetail(row.getCell(7).getStringCellValue());
				}
				if(row.getCell(8)!=null){
					row.getCell(8).setCellType(Cell.CELL_TYPE_STRING);
					torderRquest.settClentId(row.getCell(8).getStringCellValue());
				}
				if(row.getCell(9)!=null){
					row.getCell(9).setCellType(Cell.CELL_TYPE_STRING);
//					torderRquest.settClentId(row.getCell(9).getStringCellValue());
				}
				if(row.getCell(10)!=null){
					row.getCell(10).setCellType(Cell.CELL_TYPE_STRING);
					torderRquest.setgEngName(row.getCell(10).getStringCellValue());
				}
				if(row.getCell(11)!=null){
					row.getCell(11).setCellType(Cell.CELL_TYPE_STRING);
					torderRquest.setgChineseName(row.getCell(11).getStringCellValue());
				}
				if(row.getCell(12)!=null){
					row.getCell(12).setCellType(Cell.CELL_TYPE_STRING);
					torderRquest.setnMount(row.getCell(12).getStringCellValue());
				}
				list.add(torderRquest);
			}
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}

	@RequiresPermissions("mid:torderRquest:edit")
	@RequestMapping(value = "exportExcel")
	public String exportExcel(TorderRquest torderRquest, RedirectAttributes redirectAttributes) {
		List<TorderRquest> list = getList(torderRquest);
		if (list == null || list.size() == 0) {
			addMessage(redirectAttributes, "当前无订单");
		} else {
			//生成Excel
			List<String> headerList = Lists.newArrayList();
			headerList.add("订单号");
			headerList.add("收货人姓名");
			headerList.add("手机固话");
			headerList.add("收货省份");
			headerList.add("收货城市");
			headerList.add("收货地区");
			headerList.add("收货地址");
			headerList.add("身份证信息");
			headerList.add("商品条形码");
			headerList.add("商品英文名");
			headerList.add("商品中文名称");
			headerList.add("数量");
			headerList.add("快递单号");
			ExportExcel ee = new ExportExcel(null, headerList);
			for (int i = 0; i < list.size(); i++) {
				Row row = ee.addRow();
				TorderRquest tr = list.get(i);
				ee.addCell(row, 0, tr.gettOrderNo());
				ee.addCell(row, 1, tr.gettClentName());
				ee.addCell(row, 2, tr.gettPhoneNo());
				ee.addCell(row, 3, tr.gettAddressProvince());
				ee.addCell(row, 4, tr.gettAddressCity());
				ee.addCell(row, 5, tr.gettAddressProvince());
				ee.addCell(row, 6, tr.gettAddressDetail());
				ee.addCell(row, 7, tr.gettClentId());
				ee.addCell(row, 8, tr.getgBarCode());
				ee.addCell(row, 9, tr.getgEngName());
				ee.addCell(row, 10, tr.getgChineseName());
				ee.addCell(row, 11, tr.getnMount());
				ee.addCell(row, 12, tr.getWaybillNo());
			}
			try {
				ee.writeFile("target/export.xlsx");
			} catch (IOException e) {
				e.printStackTrace();
				addMessage(redirectAttributes, "导出订单失败");
			}
			ee.dispose();
		}
		addMessage(redirectAttributes, "导出订单成功");
		return "redirect:" + Global.getAdminPath() + "/mid/torderRquest/?repage";
	}

	@RequiresPermissions("mid:torderRquest:edit")
	@RequestMapping(value = "exportExcel")
	public String exportExcel(TorderRquest torderRquest, RedirectAttributes redirectAttributes) {
		List<TorderRquest> list = getList(torderRquest);
		if (list == null || list.size() == 0) {
			addMessage(redirectAttributes, "当前无订单");
		} else {
			//生成Excel
			List<String> headerList = Lists.newArrayList();
			headerList.add("订单号");
			headerList.add("收货人姓名");
			headerList.add("手机固话");
			headerList.add("收货省份");
			headerList.add("收货城市");
			headerList.add("收货地区");
			headerList.add("收货地址");
			headerList.add("身份证信息");
			headerList.add("商品条形码");
			headerList.add("商品英文名");
			headerList.add("商品中文名称");
			headerList.add("数量");
			headerList.add("快递单号");
			ExportExcel ee = new ExportExcel(null, headerList);
			for (int i = 0; i < list.size(); i++) {
				Row row = ee.addRow();
				TorderRquest tr = list.get(i);
				ee.addCell(row, 0, tr.gettOrderNo());
				ee.addCell(row, 1, tr.gettClentName());
				ee.addCell(row, 2, tr.gettPhoneNo());
				ee.addCell(row, 3, tr.gettAddressProvince());
				ee.addCell(row, 4, tr.gettAddressCity());
				ee.addCell(row, 5, tr.gettAddressProvince());
				ee.addCell(row, 6, tr.gettAddressDetail());
				ee.addCell(row, 7, tr.gettClentId());
				ee.addCell(row, 8, tr.getgBarCode());
				ee.addCell(row, 9, tr.getgEngName());
				ee.addCell(row, 10, tr.getgChineseName());
				ee.addCell(row, 11, tr.getnMount());
				ee.addCell(row, 12, tr.getWaybillNo());
			}
			try {
				ee.writeFile("target/export.xlsx");
			} catch (IOException e) {
				e.printStackTrace();
				addMessage(redirectAttributes, "导出订单失败");
			}
			ee.dispose();
		}
		addMessage(redirectAttributes, "导出订单成功");
		return "redirect:" + Global.getAdminPath() + "/mid/torderRquest/?repage";
	}

    @ModelAttribute
    public TorderRquest get(@RequestParam(required = false) String id) {
        TorderRquest entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = torderRquestService.get(id);
        }
        if (entity == null) {
            entity = new TorderRquest();
        }
        return entity;
    }

    @ModelAttribute
    public List<TorderRquest> getList(TorderRquest torderRquest) {
        return torderRquestService.getList(torderRquest);
    }

    @ModelAttribute
    public List<TorderRquest> findList(TorderRquest torderRquest) {
        return torderRquestService.findList(torderRquest);
    }

    @RequiresPermissions("mid:torderRquest:view")
    @RequestMapping(value = {"list", ""})
    public String list(TorderRquest torderRquest, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<TorderRquest> page = torderRquestService.findPage(new Page<TorderRquest>(request, response), torderRquest);
        model.addAttribute("page", page);
        return "modules/mid/torderRquestList";
    }

    //查看明细
    @RequiresPermissions("mid:torderRquest:view")
    @RequestMapping(value = "formDetail")
    public String formDetail(TorderRquest torderRquest, Model model) {
        model.addAttribute("torderRquest", torderRquest);
        TorderProduct torderProduct = new TorderProduct();
        torderProduct.settId(torderRquest.gettId());
        List<TorderProduct> list = torderProductService.findList(torderProduct);
        model.addAttribute("page", list);
        return "modules/mid/torderRquestFormDetail";
    }

    //管理员修改订单
    @RequiresPermissions("mid:torderRquest:view")
    @RequestMapping(value = "form")
    public String form(TorderRquest torderRquest, Model model) {
        model.addAttribute("torderRquest", torderRquest);
        TorderProduct torderProduct = new TorderProduct();
        torderProduct.settId(torderRquest.gettId());
        List<TorderProduct> list = torderProductService.findList(torderProduct);
        model.addAttribute("page", list);
        return "modules/mid/torderRquestForm";
    }

    @RequiresPermissions("mid:torderRquest:edit")
    @RequestMapping(value = "save")
    public String save(TorderRquest torderRquest, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, torderRquest)) {
            return form(torderRquest, model);
        }
        torderRquestService.save(torderRquest);
        addMessage(redirectAttributes, "保存运单号成功");
        return "redirect:" + Global.getAdminPath() + "/mid/torderRquest/?repage";
    }

    @RequiresPermissions("mid:torderRquest:edit")
    @RequestMapping(value = "delete")
    public String delete(TorderRquest torderRquest, RedirectAttributes redirectAttributes) {
        torderRquestService.delete(torderRquest);
        addMessage(redirectAttributes, "删除订单成功");
        return "redirect:" + Global.getAdminPath() + "/mid/torderRquest/?repage";
    }

    @RequiresPermissions("mid:torderRquest:edit")
    @RequestMapping(value = "import", method = RequestMethod.POST)
    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes, HttpServletRequest request) {
        try {
            String path = MultipartFileToFileUtils.multipartFileToFile(file, request);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:" + adminPath + "/mid/goodInfo/list?repage";
    }

    @RequiresPermissions("mid:torderRquest:edit")
    @RequestMapping(value = "exportExcel")
    public String exportExcel(TorderRquest torderRquest, RedirectAttributes redirectAttributes, HttpServletRequest request, HttpServletResponse response) {
        List<TorderRquest> list = getList(torderRquest);
        if (list == null || list.size() == 0) {
            addMessage(redirectAttributes, "当前无订单");
        } else {
            //导出Excel
            String orderTypeName = torderRquest.getOrderType();
            List<String> headerList = Lists.newArrayList();
            headerList.add("序号");
            headerList.add("订单号");
            headerList.add("收货人姓名");
            headerList.add("手机固话");
            headerList.add("收货省份");
            headerList.add("收货城市");
            headerList.add("收货地区");
            headerList.add("收货地址");
            headerList.add("身份证信息");
            headerList.add("商品条形码");
            headerList.add("商品英文名");
            headerList.add("商品中文名称");
            headerList.add("数量");
            headerList.add("快递单号");
            ExportExcel ee = new ExportExcel(null, orderTypeName + "订单", headerList);
            Sheet sheet = ee.getSheet();
            String tempOrderNo = "";
            int tempLine = 2; // 从第二行开始
            int num = 1;
            for (int i = 0; i < list.size(); i++) {
                TorderRquest tr = list.get(i);
                if (!tempOrderNo.equals(tr.gettOrderNo())) {
                    // 合并单元格
                    if (i != 0) {
                        sheet.addMergedRegion(CellRangeAddress.valueOf("$A$" + tempLine + ":$A$" + (i + 1)));
                        sheet.addMergedRegion(CellRangeAddress.valueOf("$N$" + tempLine + ":$N$" + (i + 1)));
                        num++;
                    }
                    tempOrderNo = tr.gettOrderNo();
                    tempLine = i + 2;
                }
                Row row = ee.addRow();
                ee.addCell(row, 0, num);
                ee.addCell(row, 1, tr.gettOrderNo());
                ee.addCell(row, 2, tr.gettClentName());
                ee.addCell(row, 3, tr.gettPhoneNo());
                ee.addCell(row, 4, tr.gettAddressProvince());
                ee.addCell(row, 5, tr.gettAddressCity());
                ee.addCell(row, 6, tr.gettAddressProvince());
                ee.addCell(row, 7, tr.gettAddressDetail());
                ee.addCell(row, 8, tr.gettClentId());
                ee.addCell(row, 9, tr.getgBarCode());
                ee.addCell(row, 10, tr.getgEngName());
                ee.addCell(row, 11, tr.getgChineseName());
                ee.addCell(row, 12, tr.getnMount());
                ee.addCell(row, 13, tr.getWaybillNo());
            }
            try {
                String fileNmae = orderTypeName + ".xlsx";
                ee.write(response, fileNmae);
                ee.dispose();
                addMessage(redirectAttributes, "导出订单成功");
            } catch (IOException e) {
                e.printStackTrace();
                addMessage(redirectAttributes, "导出订单失败");
            }
        }
        return "redirect:" + Global.getAdminPath() + "/mid/torderRquest/?repage";
    }

}